var prevWidth = 0;
var formTabManager = null;
var sizeHandlersInitialized = false;
var update_browser_title_active = false;
var update_browser_title_field = null;
var update_browser_title_text = null;

function save_code()
{
	var options = {
		prepareFunction: function(){phprTriggerSave();}, 
		extraFields: {redirect: 0}, 
		loadIndicator: {show: false}, 
		onBeforePost: LightLoadingIndicator.show.pass('Saving...'), 
		onComplete: LightLoadingIndicator.hide, 
		onFailure: popupAjaxError,
		onAfterUpdate: false, 
		onAfterError: false,
		onBeforeScriptEval: function(){UnloadManager.reset_changes()},
		update: 'multi'
	}

	if ($('action_info_pages') || $('sidebar_pages'))
	{
		options.onAfterUpdate = setupInfoSize;
		options.onAfterError = setupInfoSize;
	}
	
	$('form_element').sendPhpr('onSave', options); 

	return false;
}

function resize_editarea(id)
{
	editAreaLoader.toggle(id, "off");
	editAreaLoader.toggle(id, "on");
}

window.addEvent('domready', function(){
	$(document.getElement('html')).bindKeys({
		'meta+s, ctrl+s': save_code
	});
	
	prevWidth = window.getSize().x;
});

window.addEvent('onTabManagerAdded', function(manager){
	if (manager.tabs_element.id != 'action_info_tabs' && manager.tabs_element.id != 'sidebar_tabs')
	{
		formTabManager = manager;
		
		manager.addEvent('onTabClick', function(tab, page){
			page.getElements('li.code_editor textarea').each(function(ta_element){
				if ($('frame_'+ta_element.id))
					resize_editarea(ta_element.id);
			});
			
			$('form_element').getElements('li.html textarea').each(function(ta_element){
				// if (phpr_field_initialized.has(ta_element.id))
				// {
				// 	phprTriggerSave();
				// 	tinyMCE.execCommand('mceRemoveControl',false,ta_element.id);
				// 	tinyMCE.execCommand('mceAddEditor',false,ta_element.id);
				// }

				if (setupResizeHandlers && !sizeHandlersInitialized)
					setupResizeHandlers(ta_element.id);
				
				sizeHandlersInitialized = true;
			})
			
			if ($('action_info_tabs'))
				(function(){setupInfoSize();}).delay(100);
		})
	}
})

function resizeEditors()
{
	// $('form_element').getElements('li.html textarea').each(function(ta_element){
	// 	phprTriggerSave();
	// 	tinyMCE.execCommand('mceRemoveControl',false,ta_element.id);
	// })
	// 
	// $('form_element').getElements('li.html textarea').each(function(ta_element){
	// 	tinyMCE.execCommand('mceAddEditor',false,ta_element.id);
	// })

	// $('form_element').getElements('li.code_editor textarea').each(function(ta_element){
	// 	if ($('frame_'+ta_element.id))
	// 	{
	// 		var tab = ta_element.findParent('li').getParent().findParent('li');
	// 		if (!tab || !tab.hasClass('hidden'))
	// 			resize_editarea(ta_element.id);
	// 	}
	// });
}

function onFileBrowserFileClick(path)
{
	path = ls_root_url(path);
	
	var first_editor_id = false;
	var current_editor = false;
	var processed = false;

	if (formTabManager)
	{
		formTabManager.current_page.getElements('li.code_editor textarea').each(function(ta_element){
			if (phpr_codemirror_loaded.has(ta_element.id))
			{
				if (!first_editor_id)
					first_editor_id = ta_element.id;

				if (phpr_active_code_mirror == phpr_codemirror_loaded.get(ta_element.id))
					current_editor = phpr_active_code_mirror;
			}
		});
	} else
	{
		$('form_element').getElements('li.code_editor textarea').each(function(ta_element){
			if (phpr_codemirror_loaded.has(ta_element.id))
			{
				if (!first_editor_id)
					first_editor_id = ta_element.id;

				if (phpr_active_code_mirror == phpr_codemirror_loaded.get(ta_element.id))
					current_editor = phpr_active_code_mirror;
			}
		});
	}
	
	if (!current_editor && first_editor_id)
		current_editor = phpr_codemirror_loaded.get(first_editor_id);

	if (current_editor && !processed)
	{
		current_editor.editor.replaceSelection(path);
		processed = true;
	}
	
	return false;
}

window.addEvent('resize', function() {
	if (prevWidth == window.getSize().x)
		return;

	resizeEditors();
	prevWidth = window.getSize().x;
});

window.addEvent('domready', function()
{
	if(update_browser_title_active)
	{
		var name_field = $(update_browser_title_field);
		if(name_field)
		{
			name_field.addEvent('keyup', update_browser_title);
			name_field.addEvent('change', update_browser_title);
			name_field.addEvent('paste', update_browser_title);
			update_browser_title();
		}
	}
});

function update_browser_title()
{
	var new_title;
	if(name_field = $(update_browser_title_field))
	{
		new_title = update_browser_title_text + ' "' + name_field.value +'"';
		document.title = new_title + ' | LemonStand';
	}
}